<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>keep-alive</h2>

      <button @click="comName = 'Child1'">btn1</button>
      <button @click="comName = 'Child2'">btn2</button>
      <button @click="comName = 'Child3'">btn3</button>
      <hr />

      <!-- 用include 和 exclude来包含或者排除部分页面 -->
      <!-- max表示最大缓存数 -->
      <keep-alive include="c1,c2" :max="10">
        <component :is="comName"></component>
      </keep-alive>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const { createApp } = Vue;

      const Child1 = {
        // name选项给组件添加名字
        name: "c1",
        template: `
          <div>这是child1组件</div>
        `,
        mounted() {
          console.log("child1 mounted");
        },
        beforeUnmount() {
          console.log("child1 unmount");
        },
      };
      const Child2 = {
        name: "c2",
        template: `
          <div>这是child2组件</div>
        `,
        mounted() {
          console.log("child2 mounted");
        },
        beforeUnmount() {
          console.log("child2 unmount");
        },
      };
      const Child3 = {
        name: "c3",
        template: `
          <div>这是child3组件</div>
        `,
        mounted() {
          console.log("child3 mounted");
        },
        beforeUnmount() {
          console.log("child3 unmount");
        },
      };

      const app = createApp({
        data() {
          return {
            comName: "Child1",
          };
        },
        components: {
          Child1,
          Child2,
          Child3,
        },
      });

      app.mount("#app");
    </script>
  </body>
</html>
